#include "BatchCheckInRequestModel.h"

using namespace MesModel;

BatchCheckInRequestModel::BatchCheckInRequestModel() {}


void BatchCheckInRequestModel::fromJson(const QJsonObject &json)
{
    factoryNo = json["factoryNo"].toString();
    transId = json["transId"].toString();
    eventTime.fromString(json["eventTime"].toString());
    machineNo = json["machineNo"].toString();
    batch = json["batch"].toString();

    QJsonArray pcuInfoArray = json["pcuInfo"].toArray();
    for(const QJsonValue& item:qAsConst(pcuInfoArray))
    {
        BatchCheckInRequestPcuInfo pcu_info;
        pcu_info.fromJson(item.toObject());
        pcuInfo.append(pcu_info);
    }
}

QJsonObject BatchCheckInRequestModel::toJson() const
{
    QJsonObject json;

    json["factoryNo"] = factoryNo;
    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    QString transIdStr = QString("%1%2").arg(QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS")).arg(randomNumber);
    json["transId"] = transId.isEmpty()?transId:transIdStr;
    json["eventTime"] = eventTime.toString("yyyy-mm-dd hh:mm:ss");
    json["machineNo"] = eventUser;
    json["batch"] = batch;

    QJsonArray pcuInfoArray;

    for(const BatchCheckInRequestPcuInfo& item: pcuInfo)
    {
        pcuInfoArray.append(item.toJson());
    }
    json["pcuInfo"] = pcuInfoArray;

    return json;

}

void BatchCheckInRequestPcuInfo::fromJson(const QJsonObject &json)
{
    locationNo = json["locationNo"].toString();
    pcu = json["pcu"].toString();
}

QJsonObject BatchCheckInRequestPcuInfo::toJson() const
{
    QJsonObject obj;

    obj["locationNo"] = locationNo;
    obj["pcu"] = pcu;

    return obj;
}
